package com.zyz.N_反射机制.基本应用.调用运行时类的指定结构;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author: vick
 * @Date: 2025/4/14
 * @Description:
 * @version: 1.0
 */
public class Test2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        //1. 获取Student的Class对象
        Class<?> clazz = Class.forName("com.vick.N_反射机制.基本应用.调用运行时类的指定结构.Student");

        //2. 获取方法对象
        Method setNameMethod = clazz.getDeclaredMethod("setName", String.class);

        //3. 创建实例
        Object stu = clazz.newInstance();

        // 4. 调用方法
        Object vick = setNameMethod.invoke(stu, "vick");
        System.out.println("stu=" + stu);
        System.out.println(vick);//null

        Method getNameMethod = clazz.getDeclaredMethod("getName");
        Object invoke = getNameMethod.invoke(stu);
        System.out.println(invoke);
    }
}
